<?php

namespace app\api\middleware;

use app\Request;
use think\facade\Log;
use think\facade\Request as FacadeRequest;

/**
 * Api日志中间件
 * Class SystemLog
 * @package app\admin\middleware
 */
class ApiLog
{

    public function handle(Request $request, \Closure $next)
    {
        $params = $request->param();
        if (isset($params['s'])) {
            unset($params['s']);
        }
        $method = strtolower($request->method());
        $url    = $request->url();

        if (in_array($method, ['get', 'post', 'put', 'delete'])) {
            $ip   = FacadeRequest::ip();
            $data = [
                'user-token'  => session('user-token'),
                'url'         => $url,
                'method'      => $method,
                'ip'          => $ip,
                'content'     => json_encode($params, JSON_UNESCAPED_UNICODE),
                'useragent'   => $_SERVER['HTTP_USER_AGENT'],
                'create_time' => time(),
            ];
            Log::debug($data);
        }

        return $next($request);
    }
}
